package fr.blogger.ejb.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@Entity
@Table(name = "commentaire")
@TableGenerator(name = "SEQUENCE_COMMENTAIRE", initialValue = 0, table = "sequence", pkColumnName = "code", valueColumnName = "valeur", allocationSize = 1)
public class Commentaire implements Serializable {

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQUENCE_COMMENTAIRE")
	private Integer id;

	@Size(max = 4000)
	@NotNull
	@Column(name = "contenu", length = 4000, nullable = false)
	private String contenu;

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_creation", nullable = false)
	private Date dateCreation;

	@NotNull
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinColumn(name = "ref_article", nullable = false)
	private Article article;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinColumn(name = "ref_commentaire_parent", nullable = true)
	private Commentaire commentaireParent;

	@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "commentaireParent")
	public List<Commentaire> listeCommentaireFils;

	public Commentaire() {
		super();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getContenu() {
		return this.contenu;
	}

	public void setContenu(String contenu) {
		this.contenu = contenu;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Commentaire getCommentaireParent() {
		return commentaireParent;
	}

	public void setCommentaireParent(Commentaire commentaireParent) {
		this.commentaireParent = commentaireParent;
	}

	public List<Commentaire> getListeCommentaireFils() {
		return listeCommentaireFils;
	}

	public void setListeCommentaireFils(List<Commentaire> listeCommentaireFils) {
		this.listeCommentaireFils = listeCommentaireFils;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		Commentaire other = (Commentaire) obj;
		return new EqualsBuilder().append(id, other.id).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(id).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).toString();
	}

}
